package com.example.crew9.royalchef;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import de.hdodenhof.circleimageview.CircleImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.w3c.dom.Text;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.widget.Toast.LENGTH_SHORT;

public class RecipeActivity extends AppCompatActivity {

    private String cookingName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe);
        Intent intent=getIntent();
        cookingName=intent.getStringExtra("cookingName");
        //actionBar部分
        CircleImageView cookingImage=(CircleImageView)findViewById(R.id.recipe_cookingImage);
        try{
            //http请求
            Glide.with(this).load(new URL("http://10.0.2.2:8080//test//"+cookingName+".jpg")).into(cookingImage);
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        setSupportActionBar((Toolbar)findViewById(R.id.recipe_toolbar));
        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("");
        //commentButton部分
        FloatingActionButton commentButton=(FloatingActionButton)findViewById(R.id.recipe_commentButton);
        commentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(RecipeActivity.this,"commentButton:待续",LENGTH_SHORT).show();
            }
        });
        //Gson部分
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client=new OkHttpClient();
                try{
                    //http请求
                    Response response=client.newCall(new Request.Builder()
                            .url("http://10.0.2.2:8080//test//"+cookingName+".json").build()).execute();
                    String recipeJSON=response.body().string();
                    final Recipe recipe=new Gson().fromJson(recipeJSON,Recipe.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ((TextView)findViewById(R.id.recipeName)).setText(recipe.getRecipeName());
                            ((TextView)findViewById(R.id.introduction)).setText(recipe.getIntroduction());
                            ((TextView)findViewById(R.id.ingredients)).setText(recipe.getIngredients());
                            ((TextView)findViewById(R.id.courses)).setText(recipe.getCourses());
                        }
                    });

                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
